package com.chenglai.video.service.impl;

import com.chenglai.commonutils.common.DateFormatHelper;
import com.chenglai.video.dao.VideoCategoryMapper;
import com.chenglai.video.dao.VideoInfoMapper;
import com.chenglai.video.pojo.VideoCategory;
import com.chenglai.video.pojo.VideoInfo;
import com.chenglai.video.service.VideoCategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.map.HashedMap;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 类描述：
 *
 * @Author rxh
 * @Date 2023/11/16 8:57
 */
@Service
public class VideoCategoryServiceImpl implements VideoCategoryService {

    @Resource
    private VideoCategoryMapper videoCategoryMapper;

    @Resource
    private VideoInfoMapper videoInfoMapper;

    @Override
    public PageInfo getVideoCateGoryList(Integer page, Integer size, Integer shopId) {
        PageHelper.startPage(page, size);
        Example example = new Example(VideoCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("shopId", shopId);
        example.orderBy("categoryId").desc();
        return new PageInfo(videoCategoryMapper.selectByExample(example));
    }

    @Override
    public List<VideoCategory> getVideoCateGoryList(Integer shopId) {
        Example example = new Example(VideoCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("shopId", shopId);
        example.orderBy("categoryId").desc();
        return videoCategoryMapper.selectByExample(example);
    }

    @Override
    public void editVideoCategory(VideoCategory videoCategory) {
        if (videoCategory.getCategoryId() == null) {
            //添加
            videoCategory.setIsDelete(1);
            videoCategory.setCreateTime(DateFormatHelper.currentTimes());
            videoCategoryMapper.insertSelective(videoCategory);
        } else {
            //修改
            videoCategoryMapper.updateByPrimaryKeySelective(videoCategory);
        }
    }

    @Override
    public Map<String,Object> delVideoCategory(Integer categoryId) {
        Map<String,Object> data = new HashedMap<>();
        //判断该分类是否还有数据，如有 无法删除
        List<VideoInfo> list = queryVideoListByCategoryId(categoryId);
        if (list.size() > 0) {
            data.put("flag","error");
            data.put("msg","该分类关联了文章，不可删除");
            return data;
        } else {
            videoCategoryMapper.deleteByPrimaryKey(categoryId);
            data.put("flag","success");
            data.put("msg","删除成功");
            return data;
        }
    }

    @Override
    public VideoCategory categoryDetail(Integer categoryId) {
        return videoCategoryMapper.selectByPrimaryKey(categoryId);
    }

    @Override
    public List<VideoInfo> queryVideoListByCategoryId(Integer categoryId) {
        Example example = new Example(VideoInfo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("categoryId", categoryId);
        example.orderBy("videoSort").desc();
        return videoInfoMapper.selectByExample(example);
    }
}
